import request from '@/utils/request'
import { removeTokenInfo, setTokenInfo } from '@/utils/storage'
export const sendCode = (mobile) => {
  return async () => {
    await request({
      method: 'GET',
      url: `sms/codes/${mobile}`,
    })
  }
}
export const saveToken = (payload) => {
  return {
    type: 'login/token',
    payload,
  }
}
export const login = (data) => {
  return async (dispatch) => {
    const res = await request({
      method: 'POST',
      url: `authorizations`,
      data,
    })
    dispatch(saveToken(res.data))
    setTokenInfo(res.data)
  }
}

export const logout = () => {
  return (dispatch) => {
    removeTokenInfo()
    dispatch({
      type: 'login/logout',
    })
  }
}
